package com.example.anytest.gpsDemo;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.example.anytest.R;
import com.example.anytest.gpsDemo.gps.GpsLocationListener;
import com.example.anytest.gpsDemo.gps.GpsLocationManager;
import com.example.anytest.gpsDemo.gps.GpsProviderStatus;
import com.example.anytest.gpsDemo.gps.SatAngleCalculateUtil;

/**
 * @author ohou
 * @date 2018/5/15
 */

public class GpsActivity extends Activity {

    private GpsLocationManager mGpsLocationManager         = null;
    private GpsListener                     mGpsListener                = null;
    private TextView                        mResultTextView             = null;
    private TextView                        mResultTextView2            = null;
    private double                          mLocationLongitude          = 0.0;
    private double                          mLocationLatitude           = 0.0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gps);
        initData();
    }

    private void initData() {
        //6.0以上先要去动态申请权限,这里暂时手动修改
        mGpsLocationManager = GpsLocationManager.getInstances(this);
        mGpsListener = new GpsListener();
        mResultTextView = findViewById(R.id.longitude_text);
        mResultTextView2 = findViewById(R.id.latitude_text);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGpsLocationManager.start(mGpsListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mGpsLocationManager.stop();
    }

    class GpsListener implements GpsLocationListener {

        @Override
        public void UpdateLocation(Location location) {
            Log.w("zcq", "UpdateLocation:------ " );

            if(location != null){
                mLocationLongitude = Double.parseDouble(String.format("%.4f", location.getLongitude()));
                mLocationLatitude = Double.parseDouble(String.format("%.4f", location.getLatitude()));
                String degree = "°";

                mResultTextView.setText("经度:" + mLocationLongitude+degree+"   纬度:" + mLocationLatitude+degree);
                String str = "仰角:"+ SatAngleCalculateUtil.getSatElevation(mLocationLongitude, mLocationLatitude, 105.5D) + degree +"   极化角:"+ SatAngleCalculateUtil.getSatPolarizingAngle(mLocationLongitude, mLocationLatitude, 105.5D) + degree+"   方位角:"+ SatAngleCalculateUtil.getSatAzimuth(mLocationLongitude, mLocationLatitude, 105.5D) + degree;
                mResultTextView2.setText(str);
            }

        }

        @Override
        public void UpdateStatus(String provider, int status, Bundle extras) {
        }

        @Override
        public void UpdateGPSProviderStatus(int gpsStatus) {
            switch (gpsStatus) {
                case GpsProviderStatus.GPS_ENABLED:
                    //GPS OPEN
                    break;
                case GpsProviderStatus.GPS_DISABLED:
                    //GPS CLOSE
                    break;
                case GpsProviderStatus.GPS_OUT_OF_SERVICE:
                    //GPS NOT AVAILABLE
                    break;
                case GpsProviderStatus.GPS_TEMPORARILY_UNAVAILABLE:
                    //GPS temporarily unavailable
                    break;
                case GpsProviderStatus.GPS_AVAILABLE:
                    //GPS USE
                    break;
                default:
                    break;
            }

        }
    }
}
